package com.ruoyi.serviceItem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.serviceItem.domain.ServiceItem;
import com.ruoyi.serviceItem.domain.ServicePackage;
import com.ruoyi.serviceItem.domain.ServicePackageItem;
import com.ruoyi.serviceItem.mapper.ServicePackageItemMapper;
import com.ruoyi.serviceItem.service.ServicePackageService;
import com.ruoyi.serviceItem.mapper.ServicePackageMapper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
* @author Lenovo
* @description 针对表【service_package(服务套餐表)】的数据库操作Service实现
* @createDate 2025-03-19 15:49:47
*/
@Service
public class ServicePackageServiceImpl extends ServiceImpl<ServicePackageMapper, ServicePackage>
    implements ServicePackageService{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    private final ServicePackageMapper packageMapper;
    private final ServicePackageItemMapper packageItemMapper;

    public ServicePackageServiceImpl(ServicePackageMapper packageMapper,
                                     ServicePackageItemMapper packageItemMapper) {
        this.packageMapper = packageMapper;
        this.packageItemMapper = packageItemMapper;
    }

    @Override
    public List<ServicePackage> selectServicePackageList(int storeId){
        return packageMapper.selectServicePackageList(storeId);
    }

    @Override
//    @Transactional
    public int savePackage(ServicePackage servicePackage) {

        if (servicePackage == null) {
            throw new IllegalArgumentException("套餐参数不能为空");
        }

        if (servicePackage.getPackageId() == null) {
            // 新增操作
            packageMapper.insertServicePackage(servicePackage);

            //刷新Mybatis会话
            sqlSessionFactory.openSession().flushStatements();

            Long packageId = servicePackage.getPackageId();

            // 设置关联关系
            servicePackage.getPackageItems().forEach(item -> {
                item.setPackageId(packageId); // 关键步骤
            });

            // 批量插入关联表
            if (!servicePackage.getPackageItems().isEmpty()) {
                packageItemMapper.batchInsert(servicePackage.getPackageItems());
            }
            } else {
            // 更新操作：先更新基础信息
            packageMapper.updateServicePackage(servicePackage);
            // 删除旧关联
            packageItemMapper.deleteByPackageId(servicePackage.getPackageId());
            // 批量插入关联表
            if (!servicePackage.getPackageItems().isEmpty()) {
                servicePackage.getPackageItems().forEach(item ->
                        item.setPackageId(servicePackage.getPackageId())
                );
                packageItemMapper.batchInsert(servicePackage.getPackageItems());
            }
        }

        return 1;
    }

    @Override
    @Transactional
    public int deletePackageById(Long packageId) {
        // 检查是否存在关联的未完成订单
        if (checkPackageInUse(packageId)) {
            throw new ServiceException("存在关联的未完成订单，无法删除套餐");
        }

        // 删除关联表数据
        packageItemMapper.deleteByPackageId(packageId);
        // 删除主表数据
        return packageMapper.deleteServicePackageById(packageId);
    }

    private boolean checkPackageInUse(Long packageId) {
        return packageMapper.checkPackageInUse(packageId) > 0;
    }


}




